// 深拷贝
export function mergeDeep(target, ...sources) {
  if (!sources.length) {
    return target
  }
  const source = sources.shift()
  if (target === null) {
    target = {}
  }
  if (isObject(target) && isObject(source)) {
    for (const key in source) {
      if (isObject(source[key])) {
        if (typeof target[key] === 'undefined') {
          target[key] = {}
        }
        target[key] = mergeDeep(target[key], source[key])
      } else if (Array.isArray(source[key])) {
        target[key] = []
        for (let item of source[key]) {
          if (isObject(item)) {
            target[key].push(mergeDeep({}, item))
            continue
          }
          target[key].push(item)
        }
      } else {
        target[key] = source[key]
      }
    }
  }
  return mergeDeep(target, ...sources)
}

function isObject(item) {
  return (
    item &&
    typeof item === 'object' &&
    !Array.isArray(item) &&
    !(item instanceof HTMLElement) &&
    !(item instanceof CanvasRenderingContext2D) &&
    typeof item !== 'function'
  )
}

// 让dom出现在视图中
export function scrollIntoView(target, options = true) {
  const el = toValue(target)
  if (el) {
    el.scrollIntoView(options)
  }
}

// 根据文件后缀返回文件图标
export function fileTypeIcon(suffix) {
  let type = 'other'
  if (['xlsx', 'xls'].includes(suffix)) {
    type = 'excel'
  }
  if (['doc', 'docx'].includes(suffix)) {
    type = 'word'
  }
  if (['pdf'].includes(suffix)) {
    type = 'pdf'
  }
  if (['ppt', 'pptx'].includes(suffix)) {
    type = 'ppt'
  }
  if (['png', 'jpg', 'jpeg', 'gif'].includes(suffix)) {
    type = 'img'
  }
  // if (['mp4', 'mov'].includes(suffix)) {
  //   type = 'video'
  // }
  // if (['mp3'].includes(suffix)) {
  //   type = 'music'
  // }
  if (['zip', 'rar', '7z'].includes(suffix)) {
    type = 'zip'
  }
  if (['text'].includes(suffix)) {
    type = 'text'
  }
  if (suffix === 'folder') {
    type = 'folder'
  }
  return `file-${type}`
}

// 转换文件大小
export function transformFileSize(size = 0, type = 'B', returnType) {
  if (typeof size === 'string') size = parseInt(size)
  if (typeof size !== 'number') return ''
  type = type.toUpperCase()
  returnType = returnType && returnType.toUpperCase()
  const level = ['B', 'KB', 'MB', 'GB']
  const typeLevel = level.findIndex((i) => i === type)
  // 返回等级没有,默认为 GB 等级
  const returnTypeLevel = returnType ? level.findIndex((i) => i === returnType) : level.length - 1
  if (typeLevel !== -1) {
    let diff = Math.abs(returnTypeLevel - typeLevel)
    for (let i = 0; i < diff; i++) {
      if (size / 1024 < 1) {
        // 没有返回等级,定位到大于1的最大等级
        if (!returnType) {
          diff = i
        }
        break
      }
      size = size / 1024
    }
    return `${size.toFixed(2)} ${level[diff]}`
  } else {
    return ''
  }
}

// 规范路径
export function specificationPath(path = '') {
  return path.replace(/(https?:\/\/+)|(\/\/+)/g, (match, p1) => {
    if (p1) {
      // 如果是以 http:// 或 https:// 开头的 URL，修正为正确的格式 https:// 或 http://
      return p1.startsWith('https') ? 'https://' : 'http://'
    } else {
      // 如果是以 // 开头的 URL，替换为 /
      return '/'
    }
  })
}

// 是否是链接
export function isValidURL(str) {
  const pattern = new RegExp(
    '^(https?:\\/\\/)?' + // 协议 (http 或 https)
      '((([a-zA-Z0-9\\-]+)\\.)+[a-zA-Z]{2,})' + // 域名
      '(\\:\\d+)?' + // 端口 (可选)
      '(\\/[-a-zA-Z0-9@:%._\\+~#=]*)*' + // 路径 (可选)
      '(\\?[;&a-zA-Z0-9@:%_\\+.~#?&/=]*)?' + // 查询字符串 (可选)
      '(\\#[-a-zA-Z0-9@:%_\\+.~#?&/=]*)?$', // 锚点 (可选)
    'i'
  )

  return pattern.test(str)
}
